package com.mbds.accelerometer.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.mbds.accelerometer.db.SensorContract.SensorEntry;

public class SensorDB extends SQLiteOpenHelper {
	public static final String DB_NAME = "SensorDB.db";
	public static final int DB_VERSION = 1;
	private SQLiteDatabase dataBase;

	private static SensorDB singleton;

	private SensorDB(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	public static void init(Context context) {
		if (singleton == null) {
			singleton = new SensorDB(context);
		}
	}

	public static SensorDB getInstance() {
		return singleton;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// Create the table
		db.execSQL(SensorEntry.SQL_CREATE_ENTRIES);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(SensorEntry.SQL_DELETE_ENTRIES);
		onCreate(db);
	}

	public void addValues(float x, float y, float z) {
		if (dataBase != null) {
			synchronized (dataBase) {
				if (dataBase.isOpen()) {
					ContentValues cv = new ContentValues();
					cv.put(SensorEntry.COLUMN_NAME_X, x);
					cv.put(SensorEntry.COLUMN_NAME_Y, y);
					cv.put(SensorEntry.COLUMN_NAME_Z, z);

					dataBase.insert(SensorEntry.TABLE_NAME, null, cv);
				}
			}
		}
	}

	public Map<String,List<Float>> getAndDeleteValues() {

		Map<String,List<Float>> result = new HashMap<String, List<Float>>();
		if (dataBase != null && dataBase.isOpen()) {
			String[] projection = { SensorEntry.COLUMN_NAME_X,
					SensorEntry.COLUMN_NAME_Y, SensorEntry.COLUMN_NAME_Z };

			int columnIndex_X;
			int columnIndex_Y;
			int columnIndex_Z;
			
			List<Float> valuesX = new ArrayList<Float>();
			List<Float> valuesY = new ArrayList<Float>();
			List<Float> valuesZ = new ArrayList<Float>();

			synchronized (dataBase) {
				Cursor cursor = dataBase.query(SensorEntry.TABLE_NAME,
						projection, null, null, null, null, null);

				columnIndex_X = cursor
						.getColumnIndex(SensorEntry.COLUMN_NAME_X);
				columnIndex_Y = cursor
						.getColumnIndex(SensorEntry.COLUMN_NAME_Y);
				columnIndex_Z = cursor
						.getColumnIndex(SensorEntry.COLUMN_NAME_Z);
				
				if(cursor.moveToFirst()) {
					do {
						valuesX.add(cursor.getFloat(columnIndex_X));
						valuesY.add(cursor.getFloat(columnIndex_Y));
						valuesZ.add(cursor.getFloat(columnIndex_Z));

					} while (cursor.moveToNext());
				}
				
				result.put(SensorEntry.COLUMN_NAME_X,valuesX);
				result.put(SensorEntry.COLUMN_NAME_Y,valuesY);
				result.put(SensorEntry.COLUMN_NAME_Z,valuesZ);
				
				onUpgrade(dataBase, DB_VERSION, DB_VERSION);
			}
			
		}
		return result;
	}

	public void openDB() {
			dataBase = getWritableDatabase();
	}

}
